<?php require 'common/head.php'; ?>
<?php
    require '../db_connection.php';

    if (isset($_GET['act']) && $_GET['act'] == 'del') {
        $id  = is_array($_GET['id']) ? $_GET['id'] : [$_GET['id']];
        $ids = implode(',', $id);
        $sql = "DELETE FROM admins WHERE id IN ($ids)";
        $conn->query($sql);
    }
    
    $perpage = 10; 
    $start_index = ((isset($_GET['p']) ? $_GET['p'] : 1) - 1) * $perpage;
    $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
    $where = 'WHERE 1 = 1';
    if (!empty($keyword)) {
        $where .= " AND `username` LIKE '%" . $keyword . "%'";
    }

    $sql = "SELECT * FROM admins $where ORDER BY id DESC LIMIT $start_index, $perpage";
    $mysqli_res = mysqli_query($conn, $sql);
    $list = [];
    if ($mysqli_res) {
      while($row = $mysqli_res->fetch_assoc()) {
        $list[] = $row;
      }
    }
    $sql = "SELECT COUNT(*) AS total FROM admins $where";
    $mysqli_res = mysqli_query($conn, $sql);   
    $total = 0;
    if ($mysqli_res) {
      $total = $mysqli_res->fetch_assoc()['total'];
    } 

    mysqli_close($conn);
    $total_pages = ceil($total / $perpage);

    $prev_page = (isset($_GET['p']) && $_GET['p'] > 1) ? $_GET['p']
        - 1 : 1;
    $next_page = (isset($_GET['p']) && $_GET['p'] < $total_pages) ? $_GET['p']  
        + 1 : $total_pages;
    $pagination = "<div class='pagelist'>";
    
    $param = '&keyword=' . $keyword . '&cat_id=' . $cat_id . '&sex=' . $sex . '&age=' . $age;
    $pagination .= "<a href='user_list.php?p=1$param'>首页</a>";
    $pagination .= "<a href='user_list.php?p=$prev_page$param'>上一页</a>";
    for ($i = 1; $i <= $total_pages; $i++) {
        $pagination .= "<a href='user_list.php?p=$i$param'>$i</a>";
    }
    $pagination .= "<a href='user_list.php?p=$next_page$param'>下一页</a>";
    $pagination .= "<a href='user_list.php?p=$total_pages$param'>尾页</a>";
    $pagination .= "</div>";
?>
</head>
<body>
<form method="post" action="" id="listform">
  <div class="panel admin-panel">
    <div class="panel-head"><strong class="icon-reorder">用户列表</strong></div>
    <div class="padding border-bottom">
      <ul class="search" style="padding-left:10px;">
        <li>
          <input type="text" placeholder="输入用户名" value="<?php echo isset($_GET['keyword']) ? $_GET['keyword'] :''; ?>" name="keyword" class="input" style="width:250px; line-height:17px;display:inline-block" />
          <a href="javascript:void(0)" class="button border-main icon-search" onclick="changesearch()"> 搜索</a></li>
      </ul>
    </div>
    <table class="table table-hover text-center">
      <tr>
        <th width="100" style="text-align:left; padding-left:20px;">ID</th>
        <th>用户名</th>
        <th>名字</th>
        <th>邮箱</th>
        <th>角色</th>
        <th>添加时间</th>
        <th width="310">操作</th>
      </tr>
      <?php foreach($list as $row): ?>
        <tr>
            <td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id[]" value="<?php echo $row['id'];?>"/><?php echo $row['id'];?></td>
            <td><?php echo $row['username']; ?></td>
            <td><?php echo $row['true_name']; ?></td>
            <td><?php echo $row['email']; ?></td>
            <td><?php echo $row['role'] == 'gly' ? '管理员' : '志愿者'; ?></td>
            <td><?php echo $row['add_time']; ?></td>
            <td>
                <div class="button-group"> 
                    <a class="button border-main" href="user_edit.php?id=<?php echo $row['id']; ?>"><span class="icon-edit"></span> 修改</a>
                    <a class="button border-red" href="javascript:void(0)" onclick="return del(<?php echo $row['id']; ?>)"><span class="icon-trash-o"></span> 删除</a> 
                </div>
            </td>
        </tr>
     <?php endforeach; ?>
      <tr>
        <td style="text-align:left; padding:19px 0;padding-left:20px;">
        <input type="checkbox" id="checkall"/>全选</td>
        <td colspan="9" style="text-align:left;padding-left:20px;">
            <a href="javascript:void(0)" class="button border-red icon-trash-o" style="padding:5px 15px;" onclick="DelSelect()"> 删除</a>
        </td>
      </tr>
      <?php if ($total_pages) { ?>
      <tr>
        <td colspan="10">
            <?php echo $pagination; ?>
        </td>
      </tr>
      <?php } ?>
    </table>
  </div>
</form>
<script type="text/javascript">

//搜索
function changesearch(){	
    let age = $('select[name=age]').val()
    let sex = $('select[name=sex]').val()
    let keyword = $('input[name=keyword]').val()
    location.href = 'user_list.php?age='+age+'&sex='+sex+'&keyword='+keyword
}

//单个删除
function del(id){
	if (confirm("您确定要删除吗?")) {
		window.location.href = "user_list.php?act=del&id=" + id;
	}
}

//全选
$("#checkall").click(function(){ 
  $("input[name='id[]']").each(function(){
	  if (this.checked) {
		  this.checked = false;
	  }
	  else {
		  this.checked = true;
	  }
  });
})

//批量删除
function DelSelect(){
	var Checkbox = false
    let checkedIds = []
	 $("input[name='id[]']").each(function() {
	  if (this.checked == true) {		
            Checkbox = true;	
            checkedIds.push(this.value)
	  }
	});
	if (Checkbox){
		var t = confirm("您确认要删除选中的内容吗？");
		if (t == false) return false;		
        window.location.href = "user_list.php?act=del&id=" + checkedIds.join(',');	
	} else {
		alert("请选择您要删除的内容!");
		return false;
	}
}
</script>
</body>
</html>